
//
//  MyWalletViewController.m
//  EcmDoctor
//
//  Created by Mac on 16/2/23.
//  Copyright © 2016年 张进. All rights reserved.
//

#import "MyWalletViewController.h"
#import "AppUtils.h"
#import "MyWalletTableViewCell.h"
#import "MyWalletm.h"
#import "UIImageView+AFNetworking.h"
@interface MyWalletViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    __weak IBOutlet NSLayoutConstraint *severWidth;
    
    __weak IBOutlet UILabel *_moneyLabel;
    __weak IBOutlet UILabel *_doctorName;
    __weak IBOutlet UIImageView *_iconImage;
    __weak IBOutlet UIView *_iconView;
    __weak IBOutlet UITableView *_detailMoneyTv;
    __weak IBOutlet NSLayoutConstraint *imageHeght;
    __weak IBOutlet NSLayoutConstraint *drugWidth;
    __weak IBOutlet UIScrollView *_monthScv;
    UIView *_tishiView;
    NSArray *monthAry;
    UIView *_tView;
    NSMutableArray *_dataAry;
    NSArray *_askStyAry;
    UILabel *_nonlabel;
    
}
@end

@implementation MyWalletViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
    // Do any additional setup after loading the view.
    self.navigationItem.titleView=[AppUtils generateNavigationItemTitleViewWithTitle:@"我的钱包"];

     self.view.backgroundColor = [AppUtils getBackground];
    CGFloat wh;
    if(kScreenHeight<667)
    {
        
        wh=(kScreenHeight-100)/2;
      
    }
    else if(kScreenHeight==667)
    {
        wh=(kScreenHeight-150)/2;
      
        
    }
    else
    {
        wh=(kScreenHeight-180)/2;
       
    }

    imageHeght.constant=wh;
    severWidth.constant=kScreenWidth/2;
    drugWidth.constant=kScreenWidth/2;
    _monthScv.contentSize=CGSizeMake(kScreenWidth/3*12,60);
    _detailMoneyTv.separatorStyle=UITableViewCellSeparatorStyleNone;
    _detailMoneyTv.rowHeight=80;
    _iconView.layer.borderColor=[UIColor colorWithRed:228/255.0 green:211/255.0 blue:185/255.0 alpha:1].CGColor;
    monthAry=@[@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12"];
    UIImage *tableIcon = [UIImage imageNamed:@"btn_back"];
    self.navigationItem.leftBarButtonItem =[[UIBarButtonItem alloc] initWithImage:tableIcon style:UIBarButtonItemStylePlain target:self action:@selector(changeToNavRootController:)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor colorWithPatternImage:tableIcon];
    NSUserDefaults *doctorUser=[NSUserDefaults standardUserDefaults];
    
    [_iconImage setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://ecm-user.oss-cn-beijing.aliyuncs.com/image/%@",[doctorUser objectForKey:APP_DOCTORICON_KEY]]] placeholderImage:[UIImage imageNamed:@"icon"]];
      _doctorName.text=[NSString stringWithFormat:@"%@医生",[doctorUser objectForKey:APP_DOCTORNAME_KEY]];
  _askStyAry=@[@"免费义诊",@"在线问诊",@"电话问诊",@"门诊加号"];
    //获取月份
    [self getMonth];
}
-(void)changeToNavRootController:(id)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
static int bz=0;
-(void)getMonth
{
    NSArray *ary=@[@"一月",@"二月",@"三月",@"四月",@"五月",@"六月",@"七月",@"八月",@"九月",@"十月",@"十一月",@"十二月"];
    NSDate *currentDate = [NSDate date];
    NSDateFormatter *datef =[[NSDateFormatter alloc]init];
    [datef setDateFormat:@"yyyy年MM月dd日"];
   NSString *_strData = [datef stringFromDate:currentDate];

    for(int i=1;i<=12;i++)
    {
        
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame=CGRectMake(kScreenWidth/3*(i-1), 0, kScreenWidth/3, 60);
        [btn setTitle:ary[i-1] forState:UIControlStateNormal];
        btn.tag=3000+i;
       
        if([[_strData substringWithRange:NSMakeRange(5,2)] isEqualToString:monthAry[i-1]])
        {  [btn setTitleColor:[UIColor colorWithRed:201/255.0 green:142/255.0 blue:2/255.0 alpha:1] forState:UIControlStateNormal];
            btn.backgroundColor=[UIColor colorWithRed:251/255.0 green:249/255.0 blue:245/255.0 alpha:1];
            [self reloadtableViewWith:monthAry[i-1]];
            bz=(int)btn.tag;
            _monthScv.contentOffset=CGPointMake(kScreenWidth/3*(i-1), 0);
           
            
                    }
        else
        {
            [btn setTitleColor:[UIColor colorWithRed:111/255.0 green:110/255.0 blue:110/255.0 alpha:1] forState:UIControlStateNormal];
            btn.backgroundColor=[UIColor clearColor];
        }
        [btn addTarget:self action:@selector(switchMonth:) forControlEvents:UIControlEventTouchUpInside];
        
        [_monthScv addSubview:btn];
        
    }
}
-(void)reloadtableViewWith:(NSString *)month
{
    NSString *docId= [SSKeychain passwordForService:APP_SERVICE_NAME account:APP_DOCTORINFOID_KEY];
    [MyWalletm requetDoctorWalletWith:docId And:month success:^(NSArray *detalwtmAry) {
        _dataAry =[NSMutableArray arrayWithArray:detalwtmAry];
        float money=0.00;
        for(MyWalletm *mywallet in _dataAry)
        {
            money+=mywallet.money.floatValue;
        }
        _moneyLabel.text=[NSString stringWithFormat:@"%.2f",money];
        [_detailMoneyTv reloadData];
    } failure:^(NSError *error) {
        NSLog(@"----------%@",error.localizedDescription);
    }];
    

}
-(void)switchMonth:(UIButton *)btn
{
    if(btn.tag!=bz)
    {
        [self reloadtableViewWith:monthAry[btn.tag-3001]];
        UIButton *bfbtn=(UIButton*)[self.view viewWithTag:bz];
       bz=(int)btn.tag;
    
    [bfbtn setTitleColor:[UIColor colorWithRed:111/255.0 green:110/255.0 blue:110/255.0 alpha:1]forState:UIControlStateNormal];
    
    bfbtn.backgroundColor=[UIColor clearColor];
   
    [btn setTitleColor:[UIColor colorWithRed:201/255.0 green:142/255.0 blue:2/255.0 alpha:1] forState:UIControlStateNormal];
    btn.backgroundColor=[UIColor colorWithRed:251/255.0 green:249/255.0 blue:245/255.0 alpha:1];
    
        if(bz>=3002&&bz<3012)
        {
            [UIView animateWithDuration:0.3 animations:^{
                
                _monthScv.contentOffset=CGPointMake(kScreenWidth/3*(bz-3002), 0);
            }];
            
            
        }

    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{  if(!_dataAry.count)
    {
        if(_nonlabel==nil)
    {
        _nonlabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 10, kScreenWidth, 30)];
      
        _nonlabel.textAlignment=NSTextAlignmentCenter;
        _nonlabel.backgroundColor=[UIColor clearColor];
        _nonlabel.text=@"暂时没有相关信息";
        _nonlabel.textColor=[UIColor blackColor];
        _nonlabel.font=[UIFont systemFontOfSize:15];
        
    }
        [tableView addSubview:_nonlabel];
        //_nonlabel.center=tableView.center;
        
    }
    else
    {
        [_nonlabel removeFromSuperview];
    }
    return _dataAry.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    UITableViewCell *cell;
    MyWalletm *mywallet=_dataAry[indexPath.row];
    MyWalletTableViewCell *mywalletcell=[tableView dequeueReusableCellWithIdentifier:@"mywallet" forIndexPath:indexPath];
    mywalletcell.selectionStyle= UITableViewCellSelectionStyleNone;
//    for(int i=0;i<4;i++)
//    {
//        if([mywallet.info_type isEqualToString:[NSString stringWithFormat:@"%d",i]])
//            mywalletcell.questionStyLabel.text=_askStyAry[i];
//    }
       mywalletcell.questionStyLabel.text=_askStyAry[mywallet.info_type.intValue];
       mywalletcell.customerName.text=mywallet.user_name;
        mywalletcell.moneyLabel.text=[NSString stringWithFormat:@"￥%@",mywallet.money];
    mywalletcell.timeLabel.text=mywallet.start_time;
    cell=mywalletcell;
    return cell;
}
-(CGFloat)labelwidthWith:(NSString *)str And:(UIFont *)font
{
   
    
    NSDictionary *fontDic = @{NSFontAttributeName:font};
   
    CGRect labelRect = [str boundingRectWithSize:CGSizeMake(MAXFLOAT,21) options:NSStringDrawingUsesLineFragmentOrigin attributes:fontDic context:nil];
    CGSize size = labelRect.size;
    return size.width;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 6.0) {
        
        //需要注意的是self.isViewLoaded是必不可少的，其他方式访问视图会导致它加载 ，在WWDC视频也忽视这一点。
        
        if (self.isViewLoaded && !self.view.window)// 是否是正在使用的视图
        {
            // Add code to preserve data stored in the views that might be
            // needed later.
            
            // Add code to clean up other strong references to the view in
            // the view hierarchy.
            self.view = nil;// 目的是再次进入时能够重新加载调用viewDidLoad函数。
        }
        
    }
    // Dispose of any resources that can be recreated.
}
static int sw=558;
- (IBAction)topSwitch:(UIButton *)sender {
    if(sender.tag!=sw)
    {
        UIButton *bfbtn=(UIButton*)[self.view viewWithTag:sw];
        if(sender.tag==559)
        {
         if(_tishiView==nil)
         {
             _tishiView =[[UIView alloc]initWithFrame:CGRectMake(0, 40+64, kScreenWidth, kScreenHeight-40)];
             _tishiView.backgroundColor=[UIColor colorWithRed:255/255.0 green:248/255.0 blue:239/255.0 alpha:1];
            
             UILabel *label=[[UILabel alloc]initWithFrame:CGRectMake(0, 20, kScreenWidth, 40)];
             label.backgroundColor=[UIColor clearColor];
             label.text=@"该功能正在开发。。。";
             label.textColor=[UIColor blackColor];
             label.textAlignment=NSTextAlignmentCenter;
             label.font=[UIFont systemFontOfSize:15];
             [_tishiView addSubview:label];
             
         }
            [self.view addSubview:_tishiView];
        }
       else
       {
           [_tishiView removeFromSuperview];
           
       }
        sw=(int)sender.tag;
        
        [bfbtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        bfbtn.backgroundColor=[UIColor colorWithRed:123/255.0 green:103/255.0 blue:94/255.0 alpha:1];

        [sender setTitleColor:[UIColor colorWithRed:255/255.0 green:204/255.0 blue:102/255.0 alpha:1] forState:UIControlStateNormal];
        sender.backgroundColor=[UIColor colorWithRed:42/255.0 green:18/255.0 blue:6/255.0 alpha:1];
        
        }

}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
